// pages/asset/address/address.js
var that,
  api = new (require('../../utils/api.js'));

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hidecheck: !1,
    fromcart: !1,
    checkindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    this.getaddresslist();
    if (options.a) {
      this.setData({ hidecheck: !0 });
    }
    if (options.p) {
      this.setData({ pay: !0, addreid: options.addreid });
    }
  },
  getaddresslist() {
    api.getaddresslist(res => {
      if (res.data.issuccess == 1) {
        that.formatlist(res.data.list);
      }
    });
  },
  formatlist(list) {
    let hascheck = !1, { addreid ,checkindex} = this.data, page = getCurrentPages();
    if (list.length > 0) {
      list.forEach((x, i) => {
        if (x.id == addreid) {
          checkindex = i
          hascheck = !0
          list[i]['check'] = 1
        } else {
          list[i]['check'] = 0
        }
      })
      if (!hascheck) {
            list.forEach((x,i)=>{
              if(x.ischeck==1){
                list[i]['check'] = 1
                checkindex = i; 
                hascheck = !0;
                return;
              }
            })
         if(!hascheck){
            list[0]['check'] = 1
            checkindex = 0;
         }
          
      }
 
      let pages = getCurrentPages(), page = pages[pages.length - 2];
      page.setData({ address: list[checkindex], addressid: list[checkindex].id, needgetaddress: !0  });
    }

    this.setData({ list, checkindex });
  },
  edit(e) {
    let { index } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../addressadd/addressadd?i=' + index,
    })
  },
  deladdress(e) {
    let { id, index,ischeck } = e.currentTarget.dataset, { list ,addreid} = this.data, that = this, openid = wx.getStorageSync("openid");
    if(ischeck==1){
      wx.showModal({
        title: '删除地址',
        content: '确认删除吗？',
        success: function (res) {
          if (res.confirm) {
            let data= {openid,id};
            api.deladdress(data, res => {
              if (res.data.issuccess == 1) {
                console.log("成功删除");
                list.splice(index, 1);
                api.setuseradress(res=>{
                })
                if (addreid == id) {
                  let pages = getCurrentPages(),page = pages[pages.length-2];  
                  page.setData({address:"",noaddress:!0,needgetaddress:!1});
                }
                  that.setData({ list });
              }
            });
          }
        },
      })
    }else{
      wx.showModal({
      title: '删除地址',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          let data= {openid,id};
          api.deladdress(data, res => {
            if (res.data.issuccess == 1) {
              console.log("成功删除");
              list.splice(index, 1);
              if (addreid == id) {
                let pages = getCurrentPages(),page = pages[pages.length-2];  
                page.setData({address:"",noaddress:!0,needgetaddress:!1});
              }
                that.setData({ list });
            }
          });
        }
      },
    })
  }
  },
  updateaddress(e) {
    var { addressid, index } = e.currentTarget.dataset, { ordernumber, list, addreid, pay, sconfirm } = this.data;
    let pages = getCurrentPages(), page = pages[pages.length - 2];
    page.setData({ address: list[index], addressid: list[index].id, needgetaddress: !0 });
 
    if (pay || sconfirm) { wx.navigateBack(); return; }
    if (addreid == addressid) { wx.navigateBack();return;}
 
  },
  chose(e) {

    let { index } = e.currentTarget.dataset, { list, checkindex } = this.data;
    if (list.length < 2) return;
    if (checkindex == index) return;
    list.map((x, i) => {
      list[i]['check'] = i == index ? 1 : 0
    })


    this.setData({ list, checkindex: index })

  },

  toadd() {
    wx.navigateTo({
      url: '../addressadd/addressadd',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (this.data.needgetaddress) {
      this.getaddresslist();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})